﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ELMApplication.Common.Const;
using ELMApplication.Common.Util;
using ELMApplication.Common.DataResource;

namespace UserLoginModule.Views.AddMatterView
{
    public partial class CW_Matter : ChildWindow
    {
        private IAddMatterPrentationModel model;
        public CW_Matter(IAddMatterPrentationModel model)
        {
            InitializeComponent();
            this.DataContext = this.model = model;
            this.Loaded += new RoutedEventHandler(CW_Matter_Loaded);
        }

        void CW_Matter_Loaded(object sender, RoutedEventArgs e)
        {
            extInfo.ClearContent();
            switch (model.DetailPrivege)
            {
                
                case PrivegeTypes.New:                    
                    extInfo.Load(model.ExtendInfo.Fields);
                    break;
                case PrivegeTypes.Edit:
                    extInfo.Load(model.AddingMatter.Attributes);
                    break;

            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            model.AddingMatter.Attributes = extInfo.GetFields();
            switch (model.DetailPrivege)
            {

                case PrivegeTypes.New:

                    BusyMessage.Show();
                    //如果存在Image类型，并有值
                    bool isNeedUploadFileFirst = false;
                    int cacheCount = extInfo.FilesCache.Count;
                    if (cacheCount > 0)
                    {
                        isNeedUploadFileFirst = true;
                    }
                    foreach (var item in model.AddingMatter.Attributes)
                    {

                        if (item.Key.FieldType == FieldTypes.Image.ToString() && !string.IsNullOrEmpty(item.Value))
                        {
                            Guid key = new Guid(item.Value);
                            if (extInfo.FilesCache.Any(c => c.Key == key))
                            {

                                byte[] datas = extInfo.FilesCache[key];
                                UploadDataResource udr = new UploadDataResource(key, datas, model.AddingMatter.MatterName + ".jpg");
                                udr.Upload();
                                cacheCount--;
                                udr.UploadedCompled += (sender1, e1) =>
                                {
                                    if (cacheCount == 0)
                                    {
                                        model.AddMatter();
                                    };
                                };
                            }
                        }
                    }
                    if (!isNeedUploadFileFirst)
                    {
                        model.AddMatter();
                    }
                    
                  
                    break;
                case PrivegeTypes.Edit:
                    BusyMessage.Show();
                    //如果存在Image类型，并有值
                     isNeedUploadFileFirst = false;
                     cacheCount = extInfo.FilesCache.Count;
                    if (cacheCount > 0)
                    {
                        isNeedUploadFileFirst = true;
                    }
                    foreach (var item in model.AddingMatter.Attributes)
                    {

                        if (item.Key.FieldType == FieldTypes.Image.ToString() && !string.IsNullOrEmpty(item.Value))
                        {
                            Guid key = new Guid(item.Value);
                            if (extInfo.FilesCache.Any(c => c.Key == key))
                            {

                                byte[] datas = extInfo.FilesCache[key];
                                UploadDataResource udr = new UploadDataResource(key, datas, model.AddingMatter.MatterName + ".jpg");
                                udr.Upload();
                                cacheCount--;
                                udr.UploadedCompled += (sender1, e1) =>
                                {
                                    if (cacheCount == 0)
                                    {
                                        model.UpdateMatter();
                                    };
                                };
                            }
                        }
                    }
                    if (!isNeedUploadFileFirst)
                    {
                        model.UpdateMatter();
                    }
                    
                    break;

            }
            
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

